/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imageviewer;

import de.editura.common.swing.imageviewer.DropTextOnImageTransferHandler;
import de.editura.common.swing.imageviewer.ImageCanvas;
import de.editura.common.swing.imageviewer.ImageViewerCListener;
import de.editura.common.swing.imageviewer.ImageViewerPanel;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 *
 * @author kschmidtner
 */
public class Main {

    /**
     * java -jar ImageViewer.jar ../../../oxygen/samples/tei/facs/PK_001.jpg
     * @param args the command line arguments
     * args[0] = path to image file, e.g. ../../../oxygen/samples/tei/facs/PK_001.jpg
     */
    public static void main(String[] args) throws InterruptedException {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         } catch(Exception e) {
              System.out.println("Error setting native LAF: " + e);
         }

        // simpleTest1(args[0]);
        simpleTest2(args[0]);
        //Todo - Doesn't work: simpleTest3(args[0]);
    }

    /**
     * Todo - Doesn't work!
     * Testing automatic zoom
     */
    private static void simpleTest1(String imgPath) {
        File imgFile = new File(imgPath);
        BufferedImage img = null;
        try {
            img = ImageIO.read(imgFile);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        ImageCanvas imgCanvas = new ImageCanvas(img);

        JFrame f = new JFrame("Image Viewer");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
        });

        JPanel p = new JPanel(true);
        p.add(imgCanvas);

        f.add(p);
        f.pack();
        f.setVisible(true);

        while (imgCanvas.getZoomX() < 500) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            imgCanvas.setZoomX(imgCanvas.getZoomX() + 20);
            imgCanvas.setOffX(imgCanvas.getOffX() + 10);
            imgCanvas.setOffY(imgCanvas.getOffY() + 10);
            imgCanvas.repaint();
        }
    }

    /**
     * Shows ImageViewerPanel
     * Todo: Forward/Backwards-Button throw Exceptions
     */
    private static void simpleTest2(String imgPath) {
        File imgFile = new File(imgPath);
        BufferedImage img = null;
        try {
            img = ImageIO.read(imgFile);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        JFrame f = new JFrame("Image Viewer");
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
        });

        ImageViewerPanel imgVP = new ImageViewerPanel();
        f.add(imgVP);
        f.pack();
        f.setVisible(true);
        imgVP.getImageCanvas().setImg(img);
        imgVP.getImageCanvas().repaint();

    }

    /**
     * Tofo: doesn't work
     * Test for Drag and Drop - TransferHandler
     */
    private static void simpleTest3(String imgPath) {
        File imgFile = new File(imgPath);
        BufferedImage img = null;
        try {
            img = ImageIO.read(imgFile);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        JFrame f = new JFrame("Image Viewer");
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
        });

        f.setLayout(new GridBagLayout());
        JTextField jtf = new JTextField("Hier steht was");
        jtf.setVisible(true);
        jtf.setDragEnabled(true);
        
        f.add(jtf);
        f.pack();

        ImageViewerPanel imgVP = new ImageViewerPanel();
        imgVP.addComponentListener(new ImageViewerCListener(imgVP));
        imgVP.getImageCanvas().setTransferHandler(new DropTextOnImageTransferHandler());
        f.add(imgVP);


        f.pack();
        f.setVisible(true);
        imgVP.getImageCanvas().setImgWithFile(img, imgFile);
        imgVP.getImageCanvas().repaint();
    }


}
